<?php

/**
 *  @FileComment            [权限管理控制器]
 *  @ProjectName             项目名称：JDshop
 *  @FileName                   文件名称：AccessController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-26 11:44:50
 *  *
 */
class AccessController extends CommonController {

        public function index() {
                $data = K('RoleNodeListView')->get_data();
                $this->assign('data', $data);
                $this->display();
        }

        public function delAccess() {
                $rid = Q('get.rid', 0, 'intval');
                $nid = Q('get.nid', 0, 'intval');
                K('Access')->del_data($rid, $nid);
                $this->success('删除权限成功！');
        }

        public function addAccess() {
                $access = K('Access');
                if (IS_POST) {
                        if ($access->create()) {
                                $access->add_data();
                                $this->success('添加权限成功！', U("Access/index"));
                        } else {
                                $this->error($access->error);
                        }
                }
                $role = K('Role')->get_data();
                $this->assign('role', $role);
                $node = K('Node')->get_tree_data();
                $this->assign('node', $node);
                $this->display();
        }

        public function node() {
                $data = K('Node')->get_tree_data();
                $this->assign('data', $data);
                $this->display();
        }

        public function addNode() {
                $node = K('Node');
                if (IS_POST) {
                        if ($node->create()) {
                                $node->add_son();
                                $this->success('添加顶级节点成功！', U('Access/node'));
                        } else {
                                $this->error($node->error);
                        }
                }
                $this->display();
        }

        public function delNode() {
                $nid = Q('get.nid', 0, 'intval');
                if (K('Node')->del_data($nid)) {
                        $this->success('删除节点成功！');
                } else {
                        $this->error('请先删除该节点下的子节点！');
                }
        }

        public function addSonNode() {
                $node = K('Node');
                if (IS_POST) {
                        if ($node->create()) {
                                $node->add_son();
                                $this->success('添加子节点成功！', U('Access/node'));
                        } else {
                                $this->error($node->error);
                        }
                }
                $nid = Q('get.pid', 0, 'intval');
                $data = $node->get_one_data($nid);
                $data['level'] = (int) $data['level'] + 1;
                $this->assign('node', $data);
                $this->display();
        }

}
